package com.bw.zg3park2212.emp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.DepartEntityItem
import com.bw.base.entity.DepartNumBean
import com.bw.base.entity.EmpEntityItem
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.DeparkNumAdpter
import com.bw.zg3park2212.adpter.EmpAdpter
import com.bw.zg3park2212.databinding.ActivityEmpBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/EmpActivity")
class EmpActivity : BaseActivity<ActivityEmpBinding,EmpViewModoel>() {
    lateinit var empAdpter: EmpAdpter
    lateinit var deparkNumAdpter: DeparkNumAdpter
    var departlist= mutableListOf<DepartNumBean>()
    var emplist= mutableListOf<EmpEntityItem>()
    override fun intitView() {
        empAdpter= EmpAdpter()
        binding.empRv.adapter=empAdpter
        binding.empRv.layoutManager=LinearLayoutManager(this)
        deparkNumAdpter= DeparkNumAdpter()
        binding.empRvDepart.adapter=deparkNumAdpter
        binding.empRvDepart.layoutManager=LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        binding.add.setOnClickListener {
            ARouter.getInstance().build("/app/EmpAddActivity").navigation()
        }
    }

    override fun intitData() {
        viewmodel.empSuccse.observe(this){
            viewmodel.depart()
            empAdpter.data.clear()
            emplist=it
            empAdpter.notifyDataSetChanged()
        }
        viewmodel.departSuccse.observe(this){
            deparkNumAdpter.data.clear()
            departlist.clear()
            emplist.forEach { empid->
                it.forEach {
                    if(empid.departmentId==it.parent){
                        empid.departmentName=it.name
                    }
                }
            }
            val map = emplist.groupBy {
                it.departmentName//根据部门分组
            }
            map.forEach { t, u ->
                departlist.add(DepartNumBean(t,u.size))
            }
            empAdpter.addData(emplist)
            deparkNumAdpter.addData(departlist)
            deparkNumAdpter.notifyDataSetChanged()
            empAdpter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        viewmodel.emp()
    }
}